#include <SFML/Graphics.hpp>
#include <Box2D/Box2D.h>
#include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include "TestEntity.h"

/** We need this to easily convert between pixel and real-world coordinates*/
static const float SCALE = 30.f;

/** Change int to string */
std::string toString(int x);

const int SCREENWIDTH = 800;
const int SCREENHEIGHT = 600;
const int FONTSIZE = 16;


/*
int main()
{

    //at global scope
    std::set<TestEntity*> ForRemoval;

    /** Prepare the Box2D world */
    b2Vec2 Gravity(0.f, 9.8f);
    b2World World(Gravity);


    /** Prepare the window */
    sf::RenderWindow Window(sf::VideoMode(SCREENWIDTH, SCREENHEIGHT, 32), "Test");
    Window.setFramerateLimit(60);
    Window.setKeyRepeatEnabled(false);

    //  Initialize the view
    //sf::View view(sf::FloatRect(200, 150, 400, 300));
    //Window.setView(view);


    //text
    sf::Font font;
    if(!font.loadFromFile("resources/arial.ttf")){}

    //add text
    sf::Text text;
    text.setFont(font);
    text.setCharacterSize(FONTSIZE);
    text.setColor(sf::Color::Black);
    text.setStyle(sf::Text::Bold);
    text.setPosition(sf::Vector2f(Window.getSize().x -200 ,10));

    //create floors and walls
    //platform top
    TestEntity test1(World, sf::Vector2f(300,100), sf::Vector2f(190,5), STATIC);
    //floor
    TestEntity test2(World,SCALE, sf::Vector2f(400,600), sf::Vector2f(400,1), STATIC);
    //left wall
    TestEntity test3(World,SCALE, sf::Vector2f(0,300), sf::Vector2f(1,600), STATIC);
    //right wall
    TestEntity test4(World,SCALE, sf::Vector2f(800,300), sf::Vector2f(1,600), STATIC);
    //platform middle
    TestEntity test5(World,SCALE, sf::Vector2f(100,400), sf::Vector2f(90,5), STATIC);

    //create boxes in a row
    for(int i = 0; i < 30; i++){
        new TestEntity(World,SCALE, sf::Vector2f(i*32+40,50), sf::Vector2f(0,0), DYNAMIC);
    }

    while (Window.isOpen())
    {
        //When mouse button is pressed, boxes are created
        if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
        {
            int MouseX = sf::Mouse::getPosition(Window).x;
            int MouseY = sf::Mouse::getPosition(Window).y;
            new TestEntity(World,SCALE, sf::Vector2f(MouseX, MouseY), sf::Vector2f(0,0), DYNAMIC);
        }

        //Destroy Box at the Mouse location
        if (sf::Mouse::isButtonPressed(sf::Mouse::Right))
        {
            //Gotta write it
        }


        //Delete one body
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)){
            if(TestEntity::EntityList.size() > 5){
                ForRemoval.insert(TestEntity::EntityList[5]);
                std::cout << "This is called once";
            }

        }



        //advance the world
        World.Step(1/60.f, 8, 3);

        sf::Event event;

        while (Window.pollEvent(event)){
            // "close requested" event: we close the window
            if (event.type == sf::Event::Closed){
                Window.close();
            }
        }


        //Update all entitites
        for(int i = 0;i != TestEntity::EntityList.size();i++) {
            TestEntity::EntityList[i]->Update();
            //set for delete if y coordinate is > 650
            if(TestEntity::EntityList[i]->Sprite.getPosition().y > 700){
                ForRemoval.insert(TestEntity::EntityList[i]);
            }
        }

        //update text with # of bodies
        text.setString("Bodies " + toString(World.GetBodyCount()) + "\n" + "Entity: " + toString(TestEntity::EntityList.size()));


        //process list for deletion
          std::set<TestEntity*>::iterator it = ForRemoval.begin();
          std::set<TestEntity*>::iterator end = ForRemoval.end();
          for (; it!=end; ++it) {
              TestEntity* tempEntity = *it;

              //delete ball... physics body is destroyed here
              delete tempEntity;

              //... and remove it from main list of balls
              std::vector<TestEntity*>::iterator it = std::find(TestEntity::EntityList.begin(), TestEntity::EntityList.end(), tempEntity);
              if ( it != TestEntity::EntityList.end() )
                  TestEntity::EntityList.erase( it );
          }

          //clear this list for next time
          ForRemoval.clear();

        //clear out the window, color it white
        Window.clear(sf::Color::White);


        for(int i = 0;i < TestEntity::EntityList.size();i++) {
            TestEntity::EntityList[i]->Render(Window);
        }


        Window.draw(text);

        Window.display();
    }

	return 0;
}



//returns a string version of the integer parameter
std::string toString(int x){
    std::string s1;
    std::stringstream out;
    out << x;
    return out.str();
}
*/
